import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';

import { Tag } from 'ant-design-vue';

import { getDictOptions } from '#/utils/dict';

// 查询条件表单配置
export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'content',
    label: '题目内容',
  },
  {
    component: 'Input',
    fieldName: 'analysis',
    label: '题目答案',
  },
  {
    component: 'Select',
    componentProps: {
      options: [
        { label: '选择题', value: '选择题' },
        { label: '简答题', value: '简答题' },
        { label: '编程题', value: '编程题' },
      ],
    },
    fieldName: 'type',
    label: '题目类型',
  },
  {
    component: 'Select',
    componentProps: {
      options: [
        { label: '简单', value: '简单' },
        { label: '中等', value: '中等' },
        { label: '困难', value: '困难' },
      ],
    },
    fieldName: 'difficulty',
    label: '难度等级',
  },
  {
    component: 'TreeSelect',
    fieldName: 'categoryId',
    label: '所属分类',
    componentProps: {
      placeholder: '请选择所属分类',
      treeData: [],
      fieldNames: { label: 'categoryName', value: 'categoryId' },
      treeDefaultExpandAll: false,
      treeLine: { showLeafIcon: false },
      treeNodeLabelProp: 'fullName',
      showSearch: true,
      allowClear: true,
    },
  },
  {
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
    fieldName: 'status',
    label: '状态',
  },
  {
    component: 'Select',
    componentProps: {
      options: [
        { label: '是', value: '1' },
        { label: '否', value: '0' },
      ],
    },
    fieldName: 'isHot',
    label: '热门推荐',
  },
  {
    component: 'RangePicker',
    fieldName: 'createTime',
    label: '创建时间',
  },
];

// 表格列配置
export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '题目内容',
    field: 'content',
    minWidth: 200,
  },
  {
    title: '题目类型',
    field: 'type',
    width: 100,
  },
  {
    title: '难度等级',
    field: 'difficulty',
    width: 100,
    slots: {
      default: ({ row }) => {
        const difficultyMap: Record<string, { color: string; text: string }> = {
          简单: { color: 'success', text: '简单' },
          中等: { color: 'warning', text: '中等' },
          困难: { color: 'error', text: '困难' },
        };
        const config = difficultyMap[row.difficulty] || {
          color: 'default',
          text: row.difficulty,
        };
        return <Tag color={config.color}>{config.text}</Tag>;
      },
    },
  },
  {
    title: '所属分类',
    field: 'categoryName',
    width: 120,
  },
  {
    title: '热门推荐',
    field: 'isHot',
    width: 100,
    slots: {
      default: ({ row }) => {
        const isHotMap: Record<string, { color: string; text: string }> = {
          '0': { color: 'default', text: '否' },
          '1': { color: 'error', text: '是' },
        };
        const config = isHotMap[row.isHot] || {
          color: 'default',
          text: '否',
        };
        return <Tag color={config.color}>{config.text}</Tag>;
      },
    },
  },
  {
    title: '状态',
    field: 'status',
    slots: { default: 'status' },
    width: 80,
  },
  {
    title: '创建时间',
    field: 'createTime',
    width: 160,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];
